home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_368 / popmenu / test.c < prev   
C/C++ Source or Header  |  1992-05-06  |  7KB  |  212 lines

  1. /* test.c - program to demonstrate/test the PopMenu Package */
  2. /*          by Paul T. Miller                                  */
  3. /*                (Link with popmenu_pak.o and graphics_pak.o) */
  4.  
  5. #include <intuition/intuition.h>
  6. #include "graphics_pak.h"
  7. #include "popmenu_pak.h"
  8.  
  9. #define WIN_X           20
  10. #define WIN_Y           40
  11. #define WIN_W           320
  12. #define WIN_H           150
  13.  
  14. struct NewWindow nw;
  15.  
  16. struct Window *window = NULL;
  17. struct PMenu  *pmenu = NULL;   /* make a pointer to a PMenu for each one */
  18.  
  19. /* Initialize your IntuiText and PMenuItem structures like you would any
  20.    other menu (of course, you could allocate them dynamically) */
  21. struct IntuiText pitemtext[] = {
  22.    {1, 0, JAM1, 0, 0, NULL, (UBYTE *)"Broad Sword", NULL},
  23.    {1, 0, JAM1, 0, 0, NULL, (UBYTE *)"Club", NULL},
  24.    {1, 0, JAM1, 0, 0, NULL, (UBYTE *)"Dagger", NULL},
  25.    {1, 0, JAM1, 0, 0, NULL, (UBYTE *)"Javelin", NULL},
  26.    {1, 0, JAM1, 0, 0, NULL, (UBYTE *)"Long Sword", NULL},
  27.    {1, 0, JAM1, 0, 0, NULL, (UBYTE *)"Mace", NULL},
  28.    {1, 0, JAM1, 0, 0, NULL, (UBYTE *)"Short Sword", NULL}
  29. };
  30.  
  31. /* Notice the CENTERD flag: This centers the menu item text in the menu.
  32.    Make sure you specified the ITEMTEXT flag, no other PopMenu rendering is
  33.    supported yet. Also, absence of ITEMENABLED does not yet ghost the item,
  34.    but you can't select it either. */
  35.  
  36. struct PMenuItem pitems[] = {
  37.    {&pitems[1],CENTERED|ITEMTEXT|ITEMENABLED,NULL,(APTR)&pitemtext[0],NULL},
  38.    {&pitems[2],CENTERED|ITEMTEXT|ITEMENABLED,NULL,(APTR)&pitemtext[1],NULL},
  39.    {&pitems[3],CENTERED|ITEMTEXT|ITEMENABLED,NULL,(APTR)&pitemtext[2],NULL},
  40.    {&pitems[4],CENTERED|ITEMTEXT|ITEMENABLED,NULL,(APTR)&pitemtext[3],NULL},
  41.    {&pitems[5],CENTERED|ITEMTEXT|ITEMENABLED,NULL,(APTR)&pitemtext[4],NULL},
  42.    {&pitems[6],CENTERED|ITEMTEXT|ITEMENABLED,NULL,(APTR)&pitemtext[5],NULL},
  43.    {NULL,      CENTERED|ITEMTEXT|ITEMENABLED,NULL,(APTR)&pitemtext[6],NULL}
  44. };
  45.  
  46. void main(void);
  47. void abort(char *);
  48. void setup(void);
  49. void setup_display(void);
  50. void closedown(void);
  51. void close_display(void);
  52. void handle_input(void);
  53.  
  54. void main()
  55. {
  56.    setup();
  57.    handle_input();
  58. }
  59.  
  60. void abort(txt)
  61. char *txt;
  62. {
  63.    puts(txt);
  64.    closedown();
  65.    exit(0);
  66. }
  67.  
  68. void setup()
  69. {
  70.    OpenLibraries(GFXBASE|INTUITIONBASE);     /* graphics_pak.o */
  71.    setup_display();
  72. }
  73.  
  74. void closedown()
  75. {
  76.    close_display();
  77.    CloseLibraries();                         /* graphics_pak.o */
  78. }
  79.  
  80. void setup_display()
  81. {
  82.    ULONG flags, IDCMPflags;
  83.  
  84.    setmem(&nw, sizeof(struct NewWindow), 0L);
  85.  
  86.    /* you don't have to include the GADGETDOWN flag in your IDCMP, but
  87.       InitPMenu() turns this flag on for your window anyway (as PopMenus
  88.       are activated with GADGETDOWN messages) */
  89.  
  90.    IDCMPflags = RAWKEY | GADGETUP | GADGETDOWN | CLOSEWINDOW;
  91.    flags = WINDOWDRAG | WINDOWDEPTH | SMART_REFRESH | WINDOWCLOSE | ACTIVATE;
  92.  
  93.    /* set up your list-window like any other window. */
  94.    /* make sure your window is tall enough for all of the menu items, and
  95.       that they won't run off the edge of the window */
  96.  
  97.    nw.LeftEdge = WIN_X;
  98.    nw.TopEdge = WIN_Y;
  99.    nw.Width = WIN_W;
  100.    nw.Height = WIN_H;
  101.    nw.DetailPen = -1;
  102.    nw.BlockPen = -1;
  103.    nw.IDCMPFlags = IDCMPflags;
  104.    nw.Flags = flags;
  105.    nw.Title = "A Window with a PopMenu";
  106.    nw.FirstGadget = NULL;
  107.    nw.Screen = NULL;
  108.    nw.Type = WBENCHSCREEN;
  109.  
  110.    /* open your window - PopMenus must be attached to windows */
  111.  
  112.    window = (struct Window *)OpenWindow(&nw);
  113.    if (!window)
  114.       abort("Can't open PopMenu Window.");
  115.  
  116.    /* allocate your PopMenu - use type COMMPMENU, LISTPMENUs are not
  117.       yet supported. Also, make the first item the active (visible) one.
  118.       The NULL means we don't want a default title - we just show the
  119.       selected item name in the menu box. We'll use 100 for this PopMenu ID.
  120.       Use this ID to find out which PopMenu was selected (DO NOT USE ANY
  121.       GADGETS IN YOUR PROGRAM WITH THE SAME ID AS ANY POPMENUs). */
  122.  
  123.    pmenu = BuildPMenu(&pitems[0], COMMPMENU|SHADOWED, 1, NULL, 100);
  124.    if (!pmenu)
  125.       abort("Can't allocate the PopMenu.");
  126.  
  127.    /* you can make constant changes to the width and height of the PopMenu
  128.       before you initialize it with InitPMenu(), otherwise the width/height
  129.       will be derrived from the window's RastPort and the longest item name
  130.       length. The separation of the menu items is derrived from the PMenu
  131.       height as well, so don't use values that are too large.
  132.  
  133.       i.e.:  "pmenu->Width = 150;"
  134.              "pmenu->Height = 10;"  */
  135.  
  136.    /* Now initialize and attach your PopMenu to your window, with the
  137.       upper-left corner at 10, 20. */
  138.  
  139.    InitPMenu(window, pmenu, 50, 45);
  140. }
  141.  
  142. void close_display()
  143. {
  144.    if (pmenu)
  145.    {
  146.       RemovePMenu(pmenu);     /* remove the PopMenu from the window */
  147.       FreePMenu(pmenu);       /* deallocate the PopMenu */
  148.    }
  149.    if (window) CloseWindow(window);
  150. }
  151.  
  152. void handle_input(void)
  153. {
  154.    struct IntuiMessage *message;
  155.    ULONG class;
  156.    USHORT code, qualifier;
  157.    struct Gadget *gadget;
  158.    struct Window *win;
  159.    USHORT item;
  160.    struct PMenu *popmenu;
  161.  
  162.    /* receive and handle Intui-events normally */
  163.  
  164.    while (1)
  165.    {
  166.       WaitPort(window->UserPort);
  167.       while (message = (struct IntuiMessage *)GetMsg(window->UserPort))
  168.       {
  169.          class = message->Class;
  170.          code = message->Code;
  171.          qualifier = message->Qualifier;
  172.          win = message->IDCMPWindow;
  173.          gadget = (struct Gadget *)message->IAddress;
  174.  
  175.          ReplyMsg((struct Message *)message);
  176.  
  177.          switch (class)
  178.          {
  179.             case CLOSEWINDOW:
  180.                abort("");
  181.                break;
  182.  
  183.          /* when you get a GADGETDOWN message, look at the GadgetID of the
  184.             returned gadget. If it matches one of your PopMenu ID's, pass
  185.             a pointer to the gadget's UserData field (typecast to type
  186.             (struct PMenu *) first) to the HandlePMenu() function, which
  187.             will draw the menu and let the user select (or not) an item */
  188.  
  189.             case GADGETDOWN:
  190.                if (gadget->GadgetID == 100)  /* test to see if a PopMenu */
  191.                {
  192.                   popmenu = (struct PopMenu *)gadget->UserData;
  193.                   if (!popmenu) break;          /* just to be sure... */
  194.  
  195.                   item = HandlePMenu(popmenu);  /* get the item number */
  196.  
  197.                   if (item == 0)
  198.                      printf("No Item selected.\n");
  199.                   else
  200.                      printf("Item #%d Selected: %s\n", item,
  201.                                             (char *)pitemtext[item-1].IText);
  202.                }
  203.                break;
  204.             case MENUPICK:
  205.                break;
  206.             case RAWKEY:
  207.                break;
  208.          }
  209.       }
  210.    }
  211. }
  212.